<?php
	include "./utils/auth.php";
	include "./utils/dbfunctions.php";
	logToFile("getbattlestable.php");
	if (isset($_GET['order']))
	{
		$order = $_GET['order'];
	}else
	{
		$order = 3;
	}
	switch ($order)
	{
	case 0:
		$orderstring = "channelid";
		break;
	case 1:
		$orderstring = 'map';
		break;
	case 2:
		$orderstring = 'enemy';
		break;
	case 3:
		$orderstring = 'timestamp';
		break;
	case 4:
		$orderstring = 'fc';
		break;
	default:
		$orderstring = 'timestamp';
		break;
	}
?>
<table width="100%">
  <thead>
  <tr>
  	<td>ChannelID</td>
  	<td>Map</td>
  	<td>Enemy</td>
  	<td>Time</td>
  </tr>
</thead>
<tbody>
<?php
	$stmt = $db->prepare("SELECT DISTINCT channelid, map, enemy, timestamp AS ts
FROM wottools.wotcwchannel
WHERE payedout =0
ORDER BY ".$orderstring." DESC");
	$stmt->execute();

	$stmt->bind_result($channelid,$map,$enemy,$timestamp);
	while($stmt->fetch())
	{
		//echo $timestamp."<p>";
			echo '<tr>';
			echo '<td width="10%"><a href="javascript:getCurrentBattleLineupTable('.$channelid.')">';
			echo $channelid;
			echo '</a></td>';
			echo '<td width="10%">';
			echo $map;
			echo '</td>';
			echo '<td width="40%">';
			echo $enemy;
			echo '</td>';
			echo '<td width="20%">';
			echo $timestamp;
			echo '</td>';
			echo '</tr>';
	}
	$stmt->close();

  ?>
  </tbody>
</table>